<?php
session_start ();
$_SESSION ['pathway'] = '.';
require_once ("./commun/ouvre_base.php");
include_once ($_SESSION ['pathway'] . "/header.php");

if (! isset ( $_SESSION ['id'] )) {
	echo "<table class='principale'><tr><td>Session expired</td></tr>";
	echo "<a href='" . $_SESSION ['pathway'] . "/index.php'>Start</a></table>";
} elseif ($_SESSION ['acces_genetique'] < 1) {
	echo "<table class='principale'><tr><td>Restricted area</td></tr>";
	echo "<a href='" . $_SESSION ['pathway'] . "/start.php'>Home</a></table>";
} else {
	
	echo "<p><a href=\"" . $_SESSION ['pathway'] . "/index.php\">Home</a> > Genetics</p>";
	
	if (isset ( $_GET ['submit'] )) {
		$submit = $_GET ['submit'];
	} else {
		$submit = '';
	}
	if (isset ( $_GET ['type'] )) {
		$type = $_GET ['type'];
	} else {
		$type = 'linkage';
	}
	// print_r($_SESSION);
	$user = $_SESSION ['id'];
	
	if ($submit == 'yes') {
		
		$pb = '';
		
		if (isset ( $_SESSION ['code1'] )) {
			$code1 = $_SESSION ['code1'];
		}
		if (isset ( $_SESSION ['code2'] )) {
			$code2 = $_SESSION ['code2'];
		}
		if (isset ( $_SESSION ['code3'] )) {
			$code3 = $_SESSION ['code3'];
		}
		if (isset ( $_SESSION ['code4'] )) {
			$code4 = $_SESSION ['code4'];
		}
		if (isset ( $_SESSION ['code5'] )) {
			$code5 = $_SESSION ['code5'];
			$individu = $code5;
		} else {
			$individu = '';
		}
		$famille = $code1 . '-' . $code2 . '-' . $code3 . '-' . $code4;
		
		if (isset ( $_POST ['datelinkage'] )) {
			$datelinkage = $_POST ['datelinkage'];
		}
		if (isset ( $_POST ['datescreening'] )) {
			$date = $_POST ['datescreening'];
		}
		if (isset ( $_POST ['datewga'] )) {
			$date = $_POST ['datewga'];
		}
		if (isset ( $_POST ['datesample'] )) {
			$datesample = $_POST ['datesample'];
		}
		
		if ($famille != '' or isset ( $_SESSION ['codeexterne'] ) and $_SESSION ['codeexterne'] != '' or $famille != '' or isset ( $_SESSION ['codeexterne'] ) and $_SESSION ['codeexterne'] != '' or $famille != '' or isset ( $_SESSION ['codeexterne'] ) and $_SESSION ['codeexterne'] != '' or $famille != '' or isset ( $_SESSION ['codeexternesample'] ) and $_SESSION ['codeexternesample'] != '') {
			
			// echo getcwd();
			if (isset ( $_FILES ['fichiersattaches'] ) and $_FILES ['fichiersattaches'] ['error'] == 0) {
				move_uploaded_file ( $_FILES ['fichiersattaches'] ['tmp_name'], './uploads/' . basename ( $_FILES ['fichiersattaches'] ['name'] ) );
				chmod ( './uploads/' . basename ( $_FILES ['fichiersattaches'] ['name'] ), 0777 );
			}
			
			if ($type == 'linkage') {
				require_once ("./requetes/remplissage_linkage.php");
				header ( 'Location: http://localhost/SPATAX/start.php' );
			} else if ($type == 'screening') {
				if (isset ( $_SESSION ['code5'] ) and $_SESSION ['code5'] != '') {
					require_once ("./requetes/remplissage_screening.php");
					echo "<table class='principale'><tr><td><h1> Ok </h1>< /br><a href='./index.php'>Back to dashboard</a></td></tr></table>";
				} else {
					echo "<table class='principale'><tr><td>Please fill individual field</td></tr></table>";
				}
			} else if ($type == 'wga') {
				require_once ("./requetes/remplissage_wholegenomeanalysis.php");
				echo "<table class='principale'><tr><td><h1> Thank you </h1></td></tr></table>";
			} else if ($type == 'sample') {
				if (isset ( $_FILES ['fichiersattaches'] ) and $_FILES ['fichiersattaches'] ['error'] == 0) {
					move_uploaded_file ( $_FILES ['fichiersattaches'] ['tmp_name'], './uploads/' . basename ( $_FILES ['fichiersattaches'] ['name'] ) );
					chmod ( './uploads/' . basename ( $_FILES ['fichiersattaches'] ['name'] ), 0777 );
				}
				
				// sample data
				if (isset ( $_SESSION ['code5'] ) and $_SESSION ['code5'] != '') {
					if (isset ( $_SESSION ['code5'] )) {
						$code5 = $_SESSION ['code5'];
					} else {
						$code5 = '';
					}
					$table_individus_sample = explode ( ';', $code5 );
					if (isset ( $_SESSION ['codeexternesample'] )) {
						$codeexternesample = $_SESSION ['codeexternesample'];
					} else {
						$codeexternesample = '';
					}
					$table_individus_codeexterne = explode ( ';', $codeexternesample );
					if (isset ( $_POST ['wheresample'] )) {
						$wheresample = $_POST ['wheresample'];
					} else {
						$wheresample = '';
					}
					$wheresample = mysql_real_escape_string ( $wheresample );
					if (isset ( $_POST ['whysample'] )) {
						$whysample = $_POST ['whysample'];
					} else {
						$whysample = '';
					}
					$whysample = mysql_real_escape_string ( $whysample );
					if (isset ( $_POST ['neurologistsample'] )) {
						$neurologistsample = $_POST ['neurologistsample'];
					} else {
						$neurologistsample = '';
					}
					$neurologistsample = strtoupper ( $neurologistsample );
					if (isset ( $_POST ['dnasample1'] ) and $_POST ['dnasample1'] != 'other') {
						$dnasample = $_POST ['dnasample1'];
					} elseif (isset ( $_POST ['dnasample2'] )) {
						$dnasample = $_POST ['dnasample2'];
					} else {
						$dnasample = '';
					}
					$qualitysample = '';
					if (isset ( $_POST ['sourcednasample'] )) {
						$sourcednasample = $_POST ['sourcednasample'];
					} else {
						$sourcednasample = '';
					}
					if (isset ( $_POST ['qualitysample'] )) {
						foreach ( $_POST ['qualitysample'] as $qs ) {
							$qualitysample .= ' ' . $qs;
						}
					}
					if (isset ( $_POST ['concentrationsample'] )) {
						$concentrationsample = $_POST ['concentrationsample'];
					} else {
						$concentrationsample = '';
					}
					if (isset ( $_POST ['quantitemicrolitre'] )) {
						$quantitemicrolitre = $_POST ['quantitemicrolitre'];
					} else {
						$quantitemicrolitre = '';
					}
					if (isset ( $_POST ['quantitenanogramme'] )) {
						$quantitenanogramme = $_POST ['quantitenanogramme'];
					} else {
						$quantitenanogramme = '';
					}
					if (isset ( $_FILES ['fichiersattaches'] ['name'] )) {
						$fichiersattaches = $_FILES ['fichiersattaches'] ['name'];
					} else {
						$fichiersattaches = '';
					}
					if (isset ( $_POST ['datesample'] )) {
						$datesample = $_POST ['datesample'];
					} else {
						$datesample = '';
					}
					if (isset ( $_POST ['user'] )) {
						$user = $_POST ['user'];
					} else {
						$user = '';
					}
					if (isset ( $_POST ['todoprelevementtraitement1'] ) and $_POST ['todoprelevementtraitement1'] != 'other') {
						$todoprelevementtraitement = $_POST ['todoprelevementtraitement1'];
					} elseif (isset ( $_POST ['todoprelevementtraitement2'] )) {
						$todoprelevementtraitement = $_POST ['todoprelevementtraitement2'];
					} else {
						$todoprelevementtraitement = '';
					}
					if (isset ( $_POST ['testsgenetiques'] )) {
						$testsgenetiques = $_POST ['testsgenetiques'];
					} else {
						$testsgenetiques = '';
					}
					$testsgenetiques = mysql_real_escape_string ( $testsgenetiques );
					
					echo "<br />";
					foreach ( $table_individus_sample as $individusample ) {
						$array_tables = Array (
								'linkage',
								'screening',
								'wholegenomeanalysis' 
						);
						foreach ( $array_tables as $table ) {
							require ("./requetes/remplissage_sample.php");
						}
					}
					echo "<table class='principale'><tr><td><h1> Thank you ! </h1></td></tr></table>";
				} else {
					echo "<table class='principale'><tr><td>Please fill individual field</td></tr>";
				}
			}
		} else {
			echo "<table class='principale'><tr><td>Please fill a code</td></tr>";
		}
		
		echo "</div>"; // formulaire
	} else {
		
		$recupmaterial = "SELECT `materialwga`
		FROM `$db`.`wholegenomeanalysis`";
		$resultatmaterial = mysql_query ( $recupmaterial ) or die ( "Impossible de trouver liste familles table linkage : " . mysql_error () );
		$tablematerial = array ();
		while ( $ligne = mysql_fetch_row ( $resultatmaterial ) ) {
			foreach ( $ligne as $element ) {
				array_push ( $tablematerial, $element );
			}
		}
		$tablematerial = array_unique ( $tablematerial );
		$listematerial = implode ( $tablematerial, ' ' );
		
		$today = getdate ();
		$formatdate = "$today[year]-$today[mon]-$today[mday]";
		$_SESSION ['date'] = $formatdate;
		
		?>
<table class='principale'>
	<tr>
		<td><a href='./index.php' name='link' style='color: #3D6F88;'> Ok </a></td>
	</tr>
</table>
<?php
	}
}
